package com.shangcheng.server.controller;

import cn.hutool.core.date.DateTime;
import com.shangcheng.model.domain.UmsResource;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/13 18:58
 */
@RestController
@RequestMapping("/resource")
public class UmsResourceController {
    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 添加后台资源
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity createResource(@RequestBody UmsResource umsResource){
        umsResource.setCreateTime(new DateTime());
        umsResourceService.createResource(umsResource);
        return ResponseEntity.ok(new Result(200,null,"添加成功"));
    }

    /**
     * 根据id删除后台资源
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteResource(@PathVariable("id") Integer id){
        umsResourceService.deleteResource(id);
        return ResponseEntity.ok(new Result(200,null,"删除成功"));
    }

    /**
     * 分页模糊查询后台资源
     * @param params
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity resourceList(@RequestParam Map<String,String> params ,@RequestParam(defaultValue = "1") Integer pageNum,
    @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult result= umsResourceService.resourceList(params,pageNum,pageSize);
        return ResponseEntity.ok(new Result(200,result,null));
    }

    /**
     * 查询所有后台资源
     * @return
     */
    @GetMapping("/listAll")
    public ResponseEntity allResourceList(){
        List<UmsResource> list= umsResourceService.allResourceList();
       return ResponseEntity.ok(new Result(200,list,null));
    }

    /**
     * 修改后台资源
     * @param id
     * @param umsResource
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateResource(@PathVariable("id") Integer id,@RequestBody UmsResource umsResource){
        umsResource.setId(id.longValue());
        umsResourceService.updateResource(umsResource);
        return ResponseEntity.ok(new Result(200,null,"修改成功"));
    }

    /**
     * 根据id查询资源详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findUmsResourceById(@PathVariable("id") Integer id){
        UmsResource resource= umsResourceService.findUmsResourceById(id);
        return ResponseEntity.ok(new Result(200,resource,null));
    }
}
